{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>GENDER</th>\n",
       "      <th>MARRIAGE</th>\n",
       "      <th>EDU_EXPERIENCE</th>\n",
       "      <th>WORK_SIZE</th>\n",
       "      <th>WORK_POWER</th>\n",
       "      <th>IS_ILLEGAL_HIS</th>\n",
       "      <th>CURR_FREEZE_VALUE</th>\n",
       "      <th>GRADUATE_YEAR</th>\n",
       "      <th>OCCUPATION</th>\n",
       "      <th>OCCUPATION_TYPE</th>\n",
       "      <th>VIP_FLAG</th>\n",
       "      <th>GRAY_FLAG</th>\n",
       "      <th>FIVE_CLASS_TYPE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15735</th>\n",
       "      <td>51</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>99</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15741</th>\n",
       "      <td>56</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>60</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15753</th>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>70</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9</td>\n",
       "      <td>z</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15788</th>\n",
       "      <td>41</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>70</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9</td>\n",
       "      <td>z</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15797</th>\n",
       "      <td>42</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>70</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       AGE  GENDER  MARRIAGE  EDU_EXPERIENCE  WORK_SIZE  WORK_POWER  \\\n",
       "15735   51       1         2              99          2           1   \n",
       "15741   56       1         2              60          2           1   \n",
       "15753   45       1         2              70          2           1   \n",
       "15788   41       1         2              70          2           1   \n",
       "15797   42       1         3              70          3           1   \n",
       "\n",
       "       IS_ILLEGAL_HIS  CURR_FREEZE_VALUE  GRADUATE_YEAR  OCCUPATION  \\\n",
       "15735             2.0                0.0            4.0           9   \n",
       "15741             2.0                0.0            4.0           9   \n",
       "15753             2.0                0.0            3.0           9   \n",
       "15788             2.0                0.0            4.0           9   \n",
       "15797             2.0                0.0            4.0           9   \n",
       "\n",
       "      OCCUPATION_TYPE  VIP_FLAG  GRAY_FLAG  FIVE_CLASS_TYPE  \n",
       "15735               5         0          0                0  \n",
       "15741               5         0          0                0  \n",
       "15753               z         0          0                0  \n",
       "15788               z         0          0                0  \n",
       "15797               5         0          0                1  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_path = './test2.csv'\n",
    "data = pd.read_csv(file_path,index_col=0)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((504, 10), (504,))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numerical = ['AGE', 'WORK_SIZE', 'CURR_FREEZE_VALUE', 'GRADUATE_YEAR']\n",
    "\n",
    "categorical = ['EDU_EXPERIENCE', 'MARRIAGE', 'OCCUPATION', 'OCCUPATION_TYPE']\n",
    "\n",
    "binary = ['GENDER', 'WORK_POWER']\n",
    "\n",
    "train_X = data[numerical + categorical + binary]\n",
    "train_Y = data['FIVE_CLASS_TYPE']\n",
    "\n",
    "train_X.shape,train_Y.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字段|中文|类型\n",
    "--|--|--\n",
    "AGE|年龄|数值\n",
    "WORK_SIZE|劳动人口数|数值\n",
    "CURR_FREEZE_VALUE|账户冻结金额|数值\n",
    "GRADUATE_YEAR|工作年限|数值\n",
    "EDU_EXPERIENCE|最高学历|类别\n",
    "MARRIAGE|结婚|类别\n",
    "OCCUPATION|职务|类别\n",
    "OCCUPATION_TYPE|职业类型|类别\n",
    "GENDER|性别|二值\n",
    "WORK_POWER|劳动能力|二值\n",
    "IS_ILLEGAL_HIS|是否非法|删除\n",
    "VIP_FLAG|白名单客户|删除\n",
    "GRAY_FLAG|灰名单客户|删除\n",
    "FIVE_CLASS_TYPE|五级分类|目标值\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 类别型变量进行One-hot编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>EDU_EXPERIENCE_10</th>\n",
       "      <th>EDU_EXPERIENCE_20</th>\n",
       "      <th>EDU_EXPERIENCE_30</th>\n",
       "      <th>EDU_EXPERIENCE_40</th>\n",
       "      <th>EDU_EXPERIENCE_50</th>\n",
       "      <th>EDU_EXPERIENCE_60</th>\n",
       "      <th>EDU_EXPERIENCE_70</th>\n",
       "      <th>EDU_EXPERIENCE_80</th>\n",
       "      <th>EDU_EXPERIENCE_90</th>\n",
       "      <th>EDU_EXPERIENCE_99</th>\n",
       "      <th>...</th>\n",
       "      <th>OCCUPATION_4</th>\n",
       "      <th>OCCUPATION_9</th>\n",
       "      <th>OCCUPATION_TYPE_0</th>\n",
       "      <th>OCCUPATION_TYPE_1</th>\n",
       "      <th>OCCUPATION_TYPE_3</th>\n",
       "      <th>OCCUPATION_TYPE_4</th>\n",
       "      <th>OCCUPATION_TYPE_5</th>\n",
       "      <th>OCCUPATION_TYPE_6</th>\n",
       "      <th>OCCUPATION_TYPE_y</th>\n",
       "      <th>OCCUPATION_TYPE_z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15735</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15741</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15753</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15788</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15797</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       EDU_EXPERIENCE_10  EDU_EXPERIENCE_20  EDU_EXPERIENCE_30  \\\n",
       "15735                  0                  0                  0   \n",
       "15741                  0                  0                  0   \n",
       "15753                  0                  0                  0   \n",
       "15788                  0                  0                  0   \n",
       "15797                  0                  0                  0   \n",
       "\n",
       "       EDU_EXPERIENCE_40  EDU_EXPERIENCE_50  EDU_EXPERIENCE_60  \\\n",
       "15735                  0                  0                  0   \n",
       "15741                  0                  0                  1   \n",
       "15753                  0                  0                  0   \n",
       "15788                  0                  0                  0   \n",
       "15797                  0                  0                  0   \n",
       "\n",
       "       EDU_EXPERIENCE_70  EDU_EXPERIENCE_80  EDU_EXPERIENCE_90  \\\n",
       "15735                  0                  0                  0   \n",
       "15741                  0                  0                  0   \n",
       "15753                  1                  0                  0   \n",
       "15788                  1                  0                  0   \n",
       "15797                  1                  0                  0   \n",
       "\n",
       "       EDU_EXPERIENCE_99        ...          OCCUPATION_4  OCCUPATION_9  \\\n",
       "15735                  1        ...                     0             1   \n",
       "15741                  0        ...                     0             1   \n",
       "15753                  0        ...                     0             1   \n",
       "15788                  0        ...                     0             1   \n",
       "15797                  0        ...                     0             1   \n",
       "\n",
       "       OCCUPATION_TYPE_0  OCCUPATION_TYPE_1  OCCUPATION_TYPE_3  \\\n",
       "15735                  0                  0                  0   \n",
       "15741                  0                  0                  0   \n",
       "15753                  0                  0                  0   \n",
       "15788                  0                  0                  0   \n",
       "15797                  0                  0                  0   \n",
       "\n",
       "       OCCUPATION_TYPE_4  OCCUPATION_TYPE_5  OCCUPATION_TYPE_6  \\\n",
       "15735                  0                  1                  0   \n",
       "15741                  0                  1                  0   \n",
       "15753                  0                  0                  0   \n",
       "15788                  0                  0                  0   \n",
       "15797                  0                  1                  0   \n",
       "\n",
       "       OCCUPATION_TYPE_y  OCCUPATION_TYPE_z  \n",
       "15735                  0                  0  \n",
       "15741                  0                  0  \n",
       "15753                  0                  1  \n",
       "15788                  0                  1  \n",
       "15797                  0                  0  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_dummies = pd.get_dummies(data[categorical],columns=categorical)\n",
    "data_dummies.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(504, 34)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_X = pd.concat([data[numerical+binary],data_dummies],axis=1)\n",
    "train = pd.concat([train_X,train_Y],axis=1)\n",
    "train.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取卡方值\n",
    "对年龄做探索性的分箱\n",
    "\n",
    "命中率，最理想的样本选择命中率是3：1~5：1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.24404761904761904"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos_cnt = train_Y.sum()   # 命中率(坏人)\n",
    "all_cnt = train_Y.count() # 所有人\n",
    "expected_ratio = float(pos_cnt)/all_cnt\n",
    "expected_ratio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "col = 'AGE'\n",
    "target = 'FIVE_CLASS_TYPE'\n",
    "df = train[[col,target]]\n",
    "df=df.dropna()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGE  count\n",
       "0   21      1\n",
       "1   22      1\n",
       "2   23      1\n",
       "3   25      1\n",
       "4   26      3"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_count = df[col].value_counts().sort_index().reset_index().rename(columns={\"index\":col,col:\"count\"})\n",
    "df_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "      <th>hit</th>\n",
       "      <th>all</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGE  count  hit  all\n",
       "0   21      1    0    1\n",
       "1   22      1    1    1\n",
       "2   23      1    0    1\n",
       "3   25      1    0    1\n",
       "4   26      3    0    3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_count['hit']=df_count.apply(lambda a:train.loc[train[col]==a['AGE'],target].sum(),axis=1)\n",
    "df_count['all']=df_count.apply(lambda a:train.loc[train[col]==a['AGE'],target].count(),axis=1)\n",
    "df_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "      <th>hit</th>\n",
       "      <th>all</th>\n",
       "      <th>expected_cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.732143</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGE  count  hit  all  expected_cnt\n",
       "0   21      1    0    1      0.244048\n",
       "1   22      1    1    1      0.244048\n",
       "2   23      1    0    1      0.244048\n",
       "3   25      1    0    1      0.244048\n",
       "4   26      3    0    3      0.732143"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_count['expected_cnt']=df_count['all']*expected_ratio\n",
    "df_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "      <th>hit</th>\n",
       "      <th>all</th>\n",
       "      <th>expected_cnt</th>\n",
       "      <th>chi_sequare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "      <td>2.341609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.732143</td>\n",
       "      <td>0.732143</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGE  count  hit  all  expected_cnt  chi_sequare\n",
       "0   21      1    0    1      0.244048     0.244048\n",
       "1   22      1    1    1      0.244048     2.341609\n",
       "2   23      1    0    1      0.244048     0.244048\n",
       "3   25      1    0    1      0.244048     0.244048\n",
       "4   26      3    0    3      0.732143     0.732143"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def chi_sequare_cal(hit,expected_count):\n",
    "    return (hit-expected_count)**2/expected_count\n",
    "df_count['chi_sequare']=df_count.apply(lambda row:chi_sequare_cal(row['hit'],row['expected_cnt']),axis=1)\n",
    "df_count.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读入卡方值表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0.95</th>\n",
       "      <th>0.9</th>\n",
       "      <th>0.5</th>\n",
       "      <th>0.1</th>\n",
       "      <th>0.05</th>\n",
       "      <th>0.01</th>\n",
       "      <th>0.005</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.003932</td>\n",
       "      <td>0.015791</td>\n",
       "      <td>0.454936</td>\n",
       "      <td>2.705543</td>\n",
       "      <td>3.841459</td>\n",
       "      <td>6.634897</td>\n",
       "      <td>7.879439</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.102587</td>\n",
       "      <td>0.210721</td>\n",
       "      <td>1.386294</td>\n",
       "      <td>4.605170</td>\n",
       "      <td>5.991465</td>\n",
       "      <td>9.210340</td>\n",
       "      <td>10.596635</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.351846</td>\n",
       "      <td>0.584374</td>\n",
       "      <td>2.365974</td>\n",
       "      <td>6.251389</td>\n",
       "      <td>7.814728</td>\n",
       "      <td>11.344867</td>\n",
       "      <td>12.838156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.710723</td>\n",
       "      <td>1.063623</td>\n",
       "      <td>3.356694</td>\n",
       "      <td>7.779440</td>\n",
       "      <td>9.487729</td>\n",
       "      <td>13.276704</td>\n",
       "      <td>14.860259</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.145476</td>\n",
       "      <td>1.610308</td>\n",
       "      <td>4.351460</td>\n",
       "      <td>9.236357</td>\n",
       "      <td>11.070498</td>\n",
       "      <td>15.086272</td>\n",
       "      <td>16.749602</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       0.95       0.9       0.5       0.1       0.05       0.01      0.005\n",
       "1  0.003932  0.015791  0.454936  2.705543   3.841459   6.634897   7.879439\n",
       "2  0.102587  0.210721  1.386294  4.605170   5.991465   9.210340  10.596635\n",
       "3  0.351846  0.584374  2.365974  6.251389   7.814728  11.344867  12.838156\n",
       "4  0.710723  1.063623  3.356694  7.779440   9.487729  13.276704  14.860259\n",
       "5  1.145476  1.610308  4.351460  9.236357  11.070498  15.086272  16.749602"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chisqure_threshold = pd.read_csv('./chisqure_threshold.csv',index_col=0)\n",
    "chisqure_threshold.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查表,看当前分箱卡方值的区分度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "58.124037680868035"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cf=0.05\n",
    "dfree=df_count.shape[0]-1\n",
    "ctv = chisqure_threshold.loc[dfree,str(cf)]\n",
    "ctv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "AGE\tcount\thit\tall\texpected_cnt\tchi_sequare\n",
    "0\t21\t1\t0\t1\t0.244048\t0.244048\n",
    "1\t22\t1\t1\t1\t0.244048\t2.341609\n",
    "2\t23\t1\t0\t1\t0.244048\t0.244048\n",
    "3\t25\t1\t0\t1\t0.244048\t0.244048\n",
    "4\t26\t3\t0\t3\t0.732143\t0.732143\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 分箱合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def merge_shiSquare(minIndex,mergeIndex,col='AGE'):\n",
    "    df_count[col]= df_count[col].astype(np.str)\n",
    "    col_name=df_count.loc[minIndex,col]+\"~\"+df_count.loc[mergeIndex,col] # 将列名拼接\n",
    "    col_names=col_name.split(\"~\") # 切分成列表\n",
    "    col_names=[float(n) for n in col_names] # 转成数值用来排序\n",
    "    col_names.sort() # 排序\n",
    "    df_count.loc[merge_index,col]= str(col_names[0])+\"~\"+str(col_names[-1]); # 把最大值和最小值拼接成表签名\n",
    "    for c in ('count', 'hit', 'all', 'expected_cnt'): \n",
    "        df_count.loc[merge_index,c]+=df_count.loc[min_index,c] # 所有列的值相加\n",
    "    # 卡方值重新计算\n",
    "    df_count.loc[merge_index,'chi_sequare']=(df_count.loc[merge_index,'hit']-df_count.loc[merge_index,'expected_cnt'])**2/df_count.loc[merge_index,'expected_cnt']\n",
    "    df_count.drop(index=minIndex,inplace=True) # 删除被合并的值\n",
    "    #df_count=df_count.reset_index()\n",
    "    pass\n",
    "#merge_shiSquare(38,37)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "while df_count.shape[0]>5: # 保存5个分箱\n",
    "    min_index = df_count[df_count['chi_sequare']==df_count['chi_sequare'].min()].index.tolist()[0] # 最小值索引\n",
    "    if min_index>0 and min_index<df_count.shape[0]:\n",
    "        diff_sqr_val = df_count.loc[min_index-1,'chi_sequare']-df_count.loc[min_index+1,'chi_sequare'] # 根据卡方值确定合并上一行还是下一行\n",
    "    if min_index==0 or diff_sqr_val>0: # 合并的索引号\n",
    "        merge_index = min_index+1\n",
    "    else :\n",
    "        merge_index = min_index-1\n",
    "    merge_shiSquare(min_index,merge_index,'AGE') # 合并分箱\n",
    "    df_count.index=range(df_count.shape[0]) # 重置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "      <th>hit</th>\n",
       "      <th>all</th>\n",
       "      <th>expected_cnt</th>\n",
       "      <th>chi_sequare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21.0~22.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.488095</td>\n",
       "      <td>0.536876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>23.0~27.0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>1.952381</td>\n",
       "      <td>0.464576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0~57.0</td>\n",
       "      <td>467</td>\n",
       "      <td>110</td>\n",
       "      <td>467</td>\n",
       "      <td>113.970238</td>\n",
       "      <td>0.138306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>58</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>2.196429</td>\n",
       "      <td>3.578542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>59.0~75.0</td>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>18</td>\n",
       "      <td>4.392857</td>\n",
       "      <td>0.587979</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         AGE  count  hit  all  expected_cnt  chi_sequare\n",
       "0  21.0~22.0      2    1    2      0.488095     0.536876\n",
       "1  23.0~27.0      8    1    8      1.952381     0.464576\n",
       "2  28.0~57.0    467  110  467    113.970238     0.138306\n",
       "3         58      9    5    9      2.196429     3.578542\n",
       "4  59.0~75.0     18    6   18      4.392857     0.587979"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算WOE值\n",
    "\n",
    "本箱woe=ln(本箱响应/本箱未响应/(总响应/总未响应))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3228346456692913"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[good,bad]=list(data[target].value_counts())\n",
    "df_ratio = bad/good\n",
    "df_ratio"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 添加WOE "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "      <th>hit</th>\n",
       "      <th>all</th>\n",
       "      <th>expected_cnt</th>\n",
       "      <th>chi_sequare</th>\n",
       "      <th>woe</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21.0~22.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.488095</td>\n",
       "      <td>0.536876</td>\n",
       "      <td>1.130615</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>23.0~27.0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>1.952381</td>\n",
       "      <td>0.464576</td>\n",
       "      <td>-0.815295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0~57.0</td>\n",
       "      <td>467</td>\n",
       "      <td>110</td>\n",
       "      <td>467</td>\n",
       "      <td>113.970238</td>\n",
       "      <td>0.138306</td>\n",
       "      <td>-0.046640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>58</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>2.196429</td>\n",
       "      <td>3.578542</td>\n",
       "      <td>1.353759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>59.0~75.0</td>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>18</td>\n",
       "      <td>4.392857</td>\n",
       "      <td>0.587979</td>\n",
       "      <td>0.437468</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         AGE  count  hit  all  expected_cnt  chi_sequare       woe\n",
       "0  21.0~22.0      2    1    2      0.488095     0.536876  1.130615\n",
       "1  23.0~27.0      8    1    8      1.952381     0.464576 -0.815295\n",
       "2  28.0~57.0    467  110  467    113.970238     0.138306 -0.046640\n",
       "3         58      9    5    9      2.196429     3.578542  1.353759\n",
       "4  59.0~75.0     18    6   18      4.392857     0.587979  0.437468"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def cal_woe(row):\n",
    "    yi=row['hit']\n",
    "    ni=row['all']-row['hit']\n",
    "    return np.log((yi/bad)/(ni/good))\n",
    "\n",
    "df_count['woe']=df_count.apply(cal_woe,axis=1)\n",
    "df_count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 添加IV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "      <th>hit</th>\n",
       "      <th>all</th>\n",
       "      <th>expected_cnt</th>\n",
       "      <th>chi_sequare</th>\n",
       "      <th>woe</th>\n",
       "      <th>iv</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21.0~22.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.488095</td>\n",
       "      <td>0.536876</td>\n",
       "      <td>1.130615</td>\n",
       "      <td>0.006224</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>23.0~27.0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>1.952381</td>\n",
       "      <td>0.464576</td>\n",
       "      <td>-0.815295</td>\n",
       "      <td>0.008351</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28.0~57.0</td>\n",
       "      <td>467</td>\n",
       "      <td>110</td>\n",
       "      <td>467</td>\n",
       "      <td>113.970238</td>\n",
       "      <td>0.138306</td>\n",
       "      <td>-0.046640</td>\n",
       "      <td>0.001991</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>58</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>2.196429</td>\n",
       "      <td>3.578542</td>\n",
       "      <td>1.353759</td>\n",
       "      <td>0.040818</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>59.0~75.0</td>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>18</td>\n",
       "      <td>4.392857</td>\n",
       "      <td>0.587979</td>\n",
       "      <td>0.437468</td>\n",
       "      <td>0.007561</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         AGE  count  hit  all  expected_cnt  chi_sequare       woe        iv\n",
       "0  21.0~22.0      2    1    2      0.488095     0.536876  1.130615  0.006224\n",
       "1  23.0~27.0      8    1    8      1.952381     0.464576 -0.815295  0.008351\n",
       "2  28.0~57.0    467  110  467    113.970238     0.138306 -0.046640  0.001991\n",
       "3         58      9    5    9      2.196429     3.578542  1.353759  0.040818\n",
       "4  59.0~75.0     18    6   18      4.392857     0.587979  0.437468  0.007561"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def cal_iv(row):\n",
    "    yi=row['hit']\n",
    "    ni=row['all']-row['hit']\n",
    "    return (yi/bad-ni/good)*row['woe']\n",
    "\n",
    "df_count['iv']=df_count.apply(cal_iv,axis=1)\n",
    "df_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.06494628130274217"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_count['iv'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
